<?php

namespace App\Models;

use App\Models\Traits\BaseModels;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

/**
 * Class UserCertOcr
 * Description:用户身份证认证模型
 * Author: Sam
 * DateTime: 2019/1/2 10:24
 *
 * @package App\Models
 * @property int $id
 * @property int $user_id 用户id
 * @property string $id_card_front 身份证正面
 * @property string $id_card_back 身份证反面
 * @property string $name 姓名
 * @property string $num 身份证号
 * @property string $domicile_address 户籍所在地
 * @property string $issuing_authority 签发机关
 * @property int $issue_gov_code 发证机关代码
 * @property string $valid_date_begin 有效期开始
 * @property string $valid_date_end 有效期结束
 * @property int $is_valid 是否有效1有效0无效
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereDomicileAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereIdCardBack($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereIdCardFront($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereIsValid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereIssueGovCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereIssuingAuthority($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereValidDateBegin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereValidDateEnd($value)
 * @mixin \Eloquent
 * @property string $hand_cert 手持身份证url
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereHandCert($value)
 * @property int $audit 审核状态  1待审核 2审核成功 3审核失败
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereAudit($value)
 * @property int $upload 已上传到FTP的标识
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr whereUpload($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserCertOcr byUserId($user_id)
 * @property-read \App\Models\User $user
 * @property-read \App\Models\UserGlobal $userGlobalRelation
 */
class UserCertOcr extends Model
{
    use BaseModels;
    /**
     * 身份证有效
     */
    CONST IS_VALID = 1;
    /**
     * 身份证无效
     */
    CONST NOT_VALID = 0;

    protected $fillable = [
        'user_id',
        'id_card_front',
        'id_card_back',
        'hand_cert',
        'name',
        'num',
        'domicile_address',
        'issuing_authority',
        'issue_gov_code',
        'valid_date_begin',
        'valid_date_end',
        'upload',
        'is_valid',
        'audit',
        'old_kskd_user_id'
    ];

    protected $hidden = ['user_id'];

    /**
     * Description:定义身份证正面url访问器
     * Author: Sam
     * DateTime: 2019/1/10 18:37
     * @param $value
     * @return string
     */
    public function getIdCardFrontAttribute($value)
    {
        try{
            Log::debug('usercertocr-oss-value'. $value);
            if(strpos($value,'http')!==false){
                return $value;
            }
            if(Storage::disk('oss')->exists($value)){
                return Storage::disk('oss')->signUrl($value);
            }else{
                return null;
            }
        }catch (\Exception $e){
            Log::debug('usercertocr-oss-error'. json_encode($this->attributes) . '|' . $e->getMessage());
        }

    }

    /**
     * Description:定义身份证背面url访问器
     * Author: Sam
     * DateTime: 2019/1/10 18:40
     * @param $value
     * @return string
     */
    public function getIdCardBackAttribute($value)
    {
        Log::debug('usercertocr-oss-value'. $value);
        try{
            if(strpos($value,'http')!==false){
                return $value;
            }
            return Storage::disk('oss')->signUrl($value);
        }catch (\Exception $e){
            Log::debug('usercertocr-oss-error'. json_encode($this->attributes) . '|' . $e->getMessage());
        }
    }

    /**
     * Description:定义身份证信息url访问器
     * Author: Sam
     * DateTime: 2019/1/10 18:40
     * @param $value
     * @return string
     */
    public function getHandCertAttribute($value)
    {
        Log::debug('usercertocr-oss-value'. $value);
        try{
            if(strpos($value,'http')!==false){
                return $value;
            }
            if(Storage::disk('oss')->exists($value)){
                return Storage::disk('oss')->signUrl($value);
            }else{
                return null;
            }
        }catch (\Exception $e){
            Log::debug('usercertocr-oss-error'. json_encode($this->attributes) . '|' . $e->getMessage());
        }
    }

    public function scopeByUserId($query,$user_id)
    {
        return $query->where('user_id',$user_id);
    }

    /**
     * 获取关联ID
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author luwei
     * @date 2019-01-15 17:01
     */
    public function userGlobalRelation(){
        return $this->belongsTo(UserGlobal::class,'user_id','uid');
    }

    /**
     * 用户关联
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author luwei
     * @date 2019-01-15 17:01
     */
    public function user(){
        return $this->belongsTo(User::class,'user_id','id');
    }

    public function userLoanRequest(){
        return $this->belongsTo(UserLoanRequest::class,'user_id','id');
    }

}
